﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TableLayoutDetectorComponent:WorkflowComponentBase
    {
        #region props

        private TableLayoutDetectorInstruction _Instruction;
        /// <summary>an object that encapsulate instruction settings.</summary>
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction=(TableLayoutDetectorInstruction)value; }
        }

        private TableLayoutDetectorOutput _Output;
        /// <summary>an object to encapsulate output</summary>
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output=(TableLayoutDetectorOutput)value; }
        }

        /// <summary></summary>
        public Dictionary<int, List<string>> ColumnHeaderOptions
        {
            get
            {
                return this._Instruction.ColumnHeaderOptions;
            }
            set
            {
                this._Instruction.ColumnHeaderOptions = value;
            }
        }

        /// <summary>
        /// <Files>
        ///     <File DocID="0" FilePath="" />
        /// </Files>
        /// </summary>
        public string FileListInputXmlFilePath
        {
            get
            {
                return this._Instruction.FileListInputXmlFilePath;
            }
            set
            {
                this._Instruction.FileListInputXmlFilePath = value;
            }
        }

        /// <summary>
        /// <Files>
        ///     <File DocID="1" Pages="1,2,3" IsElectronic="False" IsScanned="False" />
        /// </Files>
        /// </summary>
        public string TableScanResultFile
        {
            get
            {
                return this._Instruction.TableScanResultFile;
            }
            set
            {
                this._Instruction.TableScanResultFile = value;
            }
        }

        /// <summary>
        /// <Files>
        ///     <File DocID="1">
        ///         <Page PageNumber="10">
        ///             <ColumnHeader Name="Degree" X="4" Y="12" W="12.0" H="12.0"/>
        ///         </Page>
        ///     </File>
        /// </Files>
        /// </summary>
        public string TableLayoutOutputFilePath
        {
            get
            {
                return this._Instruction.TableLayoutOutputFilePath;
            }
            set
            {
                this._Instruction.TableLayoutOutputFilePath = value;
            }
        }

        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._Instruction.LogFilePath;
            }
            set
            {
                this._Instruction.LogFilePath = value;
            }
        }

        /// <summary></summary>
        public string ErrFilePath
        {
            get
            {
                return this._Instruction.ErrFilePath;
            }
            set
            {
                this._Instruction.ErrFilePath = value;
            }
        }

        /// <summary></summary>
        public int ThreadPoolSize
        {
            get
            {
                return this._Instruction.ThreadPoolSize;
            }
            set
            {
                this._Instruction.ThreadPoolSize = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public TableLayoutDetectorComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Enumeration.ToString();
            this.ComponentName = "Table layout detector";
            this.Description = "Determine column header layout based on string match and image processing";
            this.ComponentIcon = Resources.adobe;
            this.ComponentImage = Resources.Form;

            this._Instruction =
                new TableLayoutDetectorInstruction(
                    this.ComponentID, Guid.Empty,
                    new Dictionary<int, List<string>>(), string.Empty,
                    string.Empty, string.Empty, string.Empty,
                    string.Empty, 10);
            this._Output = new TableLayoutDetectorOutput(this.ComponentID, Guid.Empty, string.Empty, 0, 0);
        }
        #endregion

        /// <summary><para>subclass must implement this method to instantiate activity from xml</para></summary>
        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TableLayoutDetectorComponent component = base.Instantiate(xNode) as TableLayoutDetectorComponent;

                XmlNodeList colHeaderOpNodes = xNode.SelectNodes("ColumnHeaderOptions/ColumnHeaderOption");
                if (colHeaderOpNodes != null && colHeaderOpNodes.Count > 0)
                {
                    component.ColumnHeaderOptions = new Dictionary<int, List<string>>();
                    foreach (XmlNode colHeaderOpNode in colHeaderOpNodes)
                    {
                        int applyOrder = int.Parse(XmlDataUtil.GetAttributeValue(colHeaderOpNode, "ApplyOrder", "1"));
                        List<string> colHeaders = new List<string>();
                        foreach (XmlNode colHeaderNode in colHeaderOpNode.ChildNodes)
                        {
                            colHeaders.Add(XmlDataUtil.GetAttributeValue(colHeaderNode, "ColumnHeader", ""));
                        }
                        component.ColumnHeaderOptions.Add(applyOrder, colHeaders);
                    }
                }

                component.FileListInputXmlFilePath = XmlDataUtil.GetAttributeValue(xNode, "FileListInputXmlFilePath", "");
                component.TableScanResultFile = XmlDataUtil.GetAttributeValue(xNode, "TableScanOutputFilePath", "");
                component.TableLayoutOutputFilePath = XmlDataUtil.GetAttributeValue(xNode, "TableLayoutOutputFilePath", "");
                component.LogFilePath = XmlDataUtil.GetAttributeValue(xNode, "LogFilePath", "");
                component.ErrFilePath = XmlDataUtil.GetAttributeValue(xNode, "ErrFilePath", "");
                component.ThreadPoolSize = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "ThreadPoolSize", "10"));

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
            }
        }

        /// <summary><para>must implementable method to serialize activity settings to xml</para></summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Component");

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.ComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentName", this.ComponentName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"Enabled",this.Enabled.ToString());

            if (this.ColumnHeaderOptions != null && ColumnHeaderOptions.Count > 0)
            {
                XmlNode colHeaderOptionsNode = XmlDataUtil.AddElement(ref xDoc, xNode, "ColumnHeaderOptions");
                foreach (int applyOrder in this.ColumnHeaderOptions.Keys)
                {
                    XmlNode colHeaderOpNode = XmlDataUtil.AddElement(ref xDoc, colHeaderOptionsNode,
                                                                     "ColumnHeaderOption");
                    XmlDataUtil.UpdateAttribute(ref xDoc, colHeaderOpNode, "ApplyOrder", applyOrder.ToString());
                    List<string> colHeaders = this.ColumnHeaderOptions[applyOrder];
                    foreach (string colHeader in colHeaders)
                    {
                        XmlNode colHeaderNode = XmlDataUtil.AddElement(ref xDoc, colHeaderOpNode, "ColumnHeader");
                        XmlDataUtil.UpdateAttribute(ref xDoc, colHeaderNode, "ColumnHeader", colHeader);
                    }
                }
            }

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileListInputXmlFilePath", this.FileListInputXmlFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableScanOutputFilePath", this.TableScanResultFile);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableLayoutOutputFilePath", this.TableLayoutOutputFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrFilePath", this.ErrFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ThreadPoolSize",this.ThreadPoolSize.ToString());

            return xNode;
        }
    }
}
